package com.jldx.common;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jldx.oa.bean._MappingKit;
import com.jldx.oa.controller.ArticleController;
import com.jldx.oa.controller.BuyOrderController;
import com.jldx.oa.controller.GoodsCategoryController;
import com.jldx.oa.controller.GoodsController;
import com.jldx.oa.controller.IndexController;
import com.jldx.oa.controller.MController;
import com.jldx.oa.controller.PartnerController;
import com.jldx.oa.controller.PersonController;
import com.jldx.oa.controller.ReportController;
import com.jldx.oa.controller.SaleOrderController;
import com.jldx.oa.controller.UserController;
import com.jldx.oa.controller.YuyueController;


public class MainConfig extends JFinalConfig {
	/**
	 * 配置JFinal常量
	 */
	@Override
	public void configConstant(Constants me) {
		//读取数据库配置文�?
		PropKit.use("config.properties");
		//设置当前是否为开发模�?
		me.setDevMode(PropKit.getBoolean("devMode"));
		//设置默认上传文件保存路径 getFile等使�?
		me.setBaseUploadPath("upload/temp/");
		//设置上传�?大限制尺�?
		//me.setMaxPostSize(1024*1024*10);
		//设置默认下载文件路径 renderFile使用
		me.setBaseDownloadPath("download");
		//设置默认视图类型
		me.setViewType(ViewType.JFINAL_TEMPLATE);
		//设置404渲染视图
		//me.setError404View("404.html");
		
		
	}
	/**
	 * 配置JFinal路由映射
	 */
	@Override
	public void configRoute(Routes me) {
		
		me.add("/", IndexController.class);
		me.add("/article",ArticleController.class);
		me.add("/user",UserController.class);
		me.add("/goods_category",GoodsCategoryController.class);
		me.add("/goods",GoodsController.class);
		me.add("/buy_order",BuyOrderController.class);
		me.add("/sale_order",SaleOrderController.class);
		me.add("/partner",PartnerController.class);
		me.add("/person",PersonController.class);
		me.add("/report",ReportController.class);
		me.add("/yuyue",YuyueController.class);
		me.add("/m",MController.class);
	}
	/**
	 * 配置JFinal插件
	 * 数据库连接池
	 * ORM
	 * 缓存等插�?
	 * 自定义插�?
	 */
	@Override
	public void configPlugin(Plugins me) {
		//配置数据库连接池插件
		DruidPlugin dbPlugin=new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		//orm映射 配置ActiveRecord插件
		ActiveRecordPlugin arp=new ActiveRecordPlugin(dbPlugin);
		arp.getEngine().addSharedMethod(StrKit.class);
		arp.setShowSql(PropKit.getBoolean("devMode"));
		arp.setDialect(new MysqlDialect());
		/********在此添加数据 -Model 映射*********/
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		arp.addSqlTemplate("oa.sql");
		_MappingKit.mapping(arp);
		me.add(dbPlugin);
		me.add(arp);
		//添加到插件列表中
		Cron4jPlugin cp = new Cron4jPlugin();
		cp.addTask("* * * * *", new PassDateTask());
		me.add(cp);
	}
	/**
	 * 配置全局拦截�?
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
	}
	/**
	 * 配置全局处理�?
	 */
	@Override
	public void configHandler(Handlers me) {

	}
	
	/**
	 * 配置模板引擎 
	 */
	@Override
	public void configEngine(Engine me) {
		me.addSharedStaticMethod(StrKit.class);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", "影楼网上商城系统");
		me.addSharedObject("app",map);
		//这里只有选择JFinal TPL的时候才�?
		//配置共享函数模板
		me.addSharedFunction("/WEB-INF/m/layout.html");
	}
	
	public static void main(String[] args) {
		JFinal.start("WebContent", 80, "/", 5);
	}
	
	public static DruidPlugin createDruidPlugin() {
		PropKit.use("config.properties");
		DruidPlugin dbPlugin=new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		return dbPlugin;
	}
	

}
